package gatperdut.chato.core.handler.room;

import gatperdut.chato.core.ChatoServer;
import gatperdut.chato.core.handler.ChatoHandler;
import gatperdut.chato.core.room.DoorController;
import gatperdut.chato.core.room.RoomController;
import gatperdut.chato.model.Chato.Room.Door;
import gatperdut.chato.model.Chato.Room.Room;

import java.util.HashMap;
import java.util.Map;

public class RoomHandler extends ChatoHandler {

    private final Map<Room, RoomController> roomToController = new HashMap<Room, RoomController>();

    private final Map<String, RoomController> roomCDOIDToController = new HashMap<String, RoomController>();

    private final Map<Door, DoorController> doorToController = new HashMap<Door, DoorController>();

    public RoomHandler( ChatoServer chatoServer ) {
        super( chatoServer );
    }

    public void loadRooms() {
        RoomController rc1;
        for ( Room r : getChatoServer().getDatasource().getChato().getRoomContainer().getRooms() ) {
            rc1 = new RoomController( getChatoServer(), r );
            getRoomToController().put( r, rc1 );
            getRoomCDOIDToController().put( r.cdoID().toURIFragment(), rc1 );
        }

        for ( RoomController rc2 : getRoomToController().values() ) {
            Room r = rc2.getRoom();
            if ( r.getN() != null ) {
                rc2.getExitsController().setnRC( getRoomToController().get( r.getN() ) );
            }
            if ( r.getE() != null ) {
                rc2.getExitsController().seteRC( getRoomToController().get( r.getE() ) );
            }
            if ( r.getS() != null ) {
                rc2.getExitsController().setsRC( getRoomToController().get( r.getS() ) );
            }
            if ( r.getW() != null ) {
                rc2.getExitsController().setwRC( getRoomToController().get( r.getW() ) );
            }
            if ( r.getU() != null ) {
                rc2.getExitsController().setuRC( getRoomToController().get( r.getU() ) );
            }
            if ( r.getD() != null ) {
                rc2.getExitsController().setdRC( getRoomToController().get( r.getD() ) );
            }
        }

    }

    public RoomController findRoomControllerByCDOID( String cdoID ) {
        return getRoomCDOIDToController().get( cdoID );
    }

    public Map<Room, RoomController> getRoomToController() {
        return roomToController;
    }

    public Map<Door, DoorController> getDoorToController() {
        return doorToController;
    }

    public Map<String, RoomController> getRoomCDOIDToController() {
        return roomCDOIDToController;
    }

}
